package arch.dao.impl.user;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import arch.dao.user.RolePrivilegeDao;
import arch.domain.user.relation.RolePrivilege;
/**
 * 
 * @author ZhaoWangLong Sep 7, 2012
 *
 */
@Repository("rolePrivilegeDao")
public class RolePrivilegeImpl implements RolePrivilegeDao {
	@Resource private JdbcTemplate jdbcTemplate;
	
	public void remove(RolePrivilege rolePrivilege) {
		String sql = "delete from ID_ROLE_PRIVILEGE where (ROLE_ID, PRIVILEGE_ID) = (?,?)";

		this.jdbcTemplate.update(sql,new Object[]{
				rolePrivilege.getRole().getRoleId(),
				rolePrivilege.getPrivilege().getId()
		});
	}

	public void persist(RolePrivilege rolePrivilege) {
		String sql = "insert into ID_ROLE_PRIVILEGE(ROLE_ID, PRIVILEGE_ID) values (?,?)";
		this.jdbcTemplate.update(sql,new Object[]{
				rolePrivilege.getRole().getRoleId(),
				rolePrivilege.getPrivilege().getId()
		});
	}

	public void persist(List<RolePrivilege> rolePrivilege) {
		String sql = "insert into ID_ROLE_PRIVILEGE(ROLE_ID, PRIVILEGE_ID) values (?,?)";
	
		this.jdbcTemplate.batchUpdate(sql, new RolePrivilegeBatchPreparedStatementSetter(rolePrivilege));
	}

	public void remove(List<RolePrivilege> rolePrivilege) {
		String sql = "delete from ID_ROLE_PRIVILEGE where (ROLE_ID, PRIVILEGE_ID) = (?,?)";

		this.jdbcTemplate.batchUpdate(sql, new RolePrivilegeBatchPreparedStatementSetter(rolePrivilege));
	}

	public void remove(String sql, Object... objects) {
		this.jdbcTemplate.update(sql,objects);
	}
}
